<?php 
class Users extends SBKH{
	function index(){
		if(!isset($_SESSION['user'])) $this->redirect('login');
		$this -> SetLayout('default');
		$this -> SetVar('page_title','List Users');
		$obj = $this -> LoadModel('users');
		$records = $obj -> getUsers();	
		$this -> SetVar('records',$records);
		$this -> SetVar('menu_focus', 'list-users');
		$this -> View('user-list','left-menu');
	}
	function search(){
		$str = $_POST['txt-search'];
		if($str == "") $this->redirect('');
		$this -> SetLayout('default');
		$obj = $this -> LoadModel('users');
		$records = $obj -> getUsers(0,$str);
		
		$this -> SetVar('records',$records);
		$this -> SetVar('page_title','List Users');
		$this -> SetVar('str',$str);
		$this -> SetVar('menu_focus', 'list-users');
		$this -> View('user-list','left-menu');
	}
	function profile($userid = 0){
		unset($_SESSION['password']);
		$this->set_layout('default');
		$objUser = $this->load_model('users');
		$user = $objUser->getUsers($userid);
		$this->set('user',$user[0]);
		$this->set('page_title','User Profile');
		$this -> SetVar('menu_focus', 'list-users');
		$this->View('profile','left-menu');
	}
	function reset_password($userid = 0){
		$objUser = $this -> LoadModel('users');
		$user = $objUser -> resetPassowrd($userid);			
		
		$this -> redirect('');
	}
	function add(){
		$this -> SetLayout('default');
		if(!empty($_POST)){//die(pr($_POST));
			$_POST['data']['users']['password'] = b64encode($_POST['data']['users']['password']);
			$_POST['data']['users']['username'] = $_POST['data']['users']['email_address'];
			$_POST['data']['users']['created_by'] = $_SESSION['user']['id'];
			$_POST['data']['users']['modified_by'] = $_SESSION['user']['id'];
			$this -> Save(($this -> data()));
			$this -> redirect('');
		}else{			
			$this -> SetVar('page_title','Add New User');
			$obj = $this -> LoadModel('users');
			$groups = $obj -> getUserGroup();
			
			$this -> SetVar('groups', $groups);
			$this -> SetVar('menu_focus', 'list-users');
			$this -> View('add', 'left-menu');
		}
	}
	function delete($userid = 0){
		$obj = $this -> LoadModel('users');
		$user = $obj -> deleteUser($userid);			
		
		$this -> redirect('');
	}
	function update_status($userid = 0){
		$obj = $this -> LoadModel('users');
		$user = $obj -> updateStatus($userid);				
		
		$this->redirect('');
	}
	function edit($userid = 0){
		if(empty($_POST)){
			$this -> SetLayout('default');
			$obj = $this -> LoadModel('users');
			$record = $obj -> getUsers($userid);			
			$groups = $obj -> getUserGroup();
				
			$this -> SetVar('groups', $groups);
			$this -> SetVar('record',$record[0]);
			$this -> SetVar('page_title','Update User Info');
			$this -> SetVar('menu_focus', 'list-users');
			$this -> View('edit','left-menu');
		}else{			
			//pr($_POST);die();
			$_POST['data']['users']['modified_by'] = $_SESSION['user']['id'];
			$this -> Update(($this -> data()));
			$this -> redirect('');
		}
		
	}
	/*
	 * Group User
	 */
	function groups(){
		if(!isset($_SESSION['user'])) $this->redirect('login');
		$this -> SetLayout('default');
		$this -> SetVar('page_title','List User Groups');
		$obj = $this -> LoadModel('users');
		$records = $obj -> getGroups();
		$this -> SetVar('records',$records);
		$this -> SetVar('menu_focus', 'list-users-group');
		$this -> view('groups','left-menu');
	}
	function group_search(){
		$str = $_POST['txt-search'];
		if($str == "") $this->redirect('groups');
		$this -> SetLayout('default');
		$obj = $this -> LoadModel('users');
		$records = $obj -> getGroups(0,$str);
	
		$this -> SetVar('records',$records);
		$this -> SetVar('page_title','Search User Groups');
		$this -> SetVar('str',$str);
		$this -> SetVar('menu_focus', 'list-users-group');
		$this -> view('groups','left-menu');
	}
	function add_group(){
		$this -> SetLayout('default');
		if(!empty($_POST)){//die(pr($_POST));
			$_POST['data']['users_group']['created_by'] = $_SESSION['users']['id'];
			$_POST['data']['users_group']['modified_by'] = $_SESSION['users']['id'];
			$this -> Save(($this -> data()));
			$this -> redirect('groups');
		}else{
			$this -> SetVar('page_title','Add New User Group');
			$this -> SetVar('menu_focus', 'list-users-group');
			$this -> view('add-group','left-menu');
		}
	}
	function edit_group($groupid = 0){
		if(empty($_POST)){
			$this -> SetLayout('default');
			$obj = $this -> LoadModel('users');
			$record = $obj -> getGroups($groupid);
			$this -> SetVar('record',$record[0]);
			$this -> SetVar('page_title','Update User Group');
			$this -> SetVar('menu_focus', 'list-users-group');
			$this -> View('edit-group','left-menu');
		}else{
			//pr($_POST);die();
			$_POST['data']['users_group']['modified_by'] = $_SESSION['user']['id'];
			$this -> Update(($this -> data()));
			$this -> redirect('groups');
		}
	
	}
	function delete_group($groupid = 0){
		$obj = $this -> LoadModel('users');
		$user = $obj -> deleteGroup($groupid);
	
		$this -> redirect('groups');
	}
	
	function login($sb_error = ""){
		$this -> SetLayout('login');
		if($sb_error != "")
			$this -> SetVar('sb_error', $sb_error);
		$this -> View('login');
	}
	function logout(){
		//$objUser = $this -> loadModel('users');
		//$objUser -> logout($_SESSION['user']['user-log']['id']);
		unset($_SESSION['user']);
		unset($_SESSION['expire']);
		unset($_SESSION['property']);
		$this->redirect("../");
	}
	function login_validate(){
		if(!empty($_POST)){
			$objUser = $this -> LoadModel('users');
			$users = $objUser -> loginValidate($_POST['data']);
			if(count($users) && $users != 0){//pr($users);die($users[0]->id);
				$_SESSION['expire'] = time() + (60 * 300);
				$_SESSION['user']['id'] = $users[0]->id;
				$_SESSION['user']['username'] = $users[0]->username;
				$_SESSION['user']['fullname'] = $users[0]->fullname;
				$_SESSION['user']['email'] = $users[0]->email_address;
				$_SESSION['user']['type'] = $users[0]->user_type_id;				
				$this->redirect("../");				
			}else{
				$this->redirect("login/" . b64encode(0) );
			}
		}else{
			$this->redirect("login");
		}
	}
	function log($page_number = ""){
		$this->set_layout('default');
		$str = "";
		if(!empty($_POST)){
			$str = $_POST['txt-search'];
			if($str == "") $this->redirect('log');
		}		
		
		$objUser = $this->load_model('users');
		$limit_start = 0;// for pagination
		$limit_end = 12;// for pagination
		$page_number = $page_number != ""?$page_number:1;// for pagination
		if($page_number != 1)// for pagination
			$limit_start = $limit_end * $page_number - $limit_end;	// for pagination
		
		$results = $objUser->getUserLog($str, $limit_start, $limit_end);
		$user_logs = $results[0];
		$number_of_rows = $results[1];// for pagination
			
		$this->set('page_number',$page_number);// for pagination
		$this->set('limit_start',$limit_start);// for pagination
		$this->set('limit_end',$limit_end);// for pagination
		$this->set('number_of_rows',$number_of_rows);// for pagination					
		
		$this->set('users', $user_logs);
		$this->set('str',$str);
		$this->set('page_title','User Log');
		$this->view('user-log');
	}
	function log_detail($id = 0,$page_number = ""){
		$this->set_layout('default');
		
		$objUser = $this->load_model('users');
		$limit_start = 0;// for pagination
		$limit_end = 12;// for pagination		
		$page_number = $page_number != ""?$page_number:1;// for pagination
		if($page_number != 1)// for pagination
			$limit_start = $limit_end * $page_number - $limit_end;	// for pagination
	
		$results = $objUser->getUserLogDetail($id, $limit_start, $limit_end);
		$user_logs = $results[0];
		$number_of_rows = $results[1];// for pagination
			
		$this->set('page_number',$page_number);// for pagination
		$this->set('limit_start',$limit_start);// for pagination
		$this->set('limit_end',$limit_end);// for pagination
		$this->set('number_of_rows',$number_of_rows);// for pagination
		
		$objUser = $this->load_model('users');
		$objUser -> saveUserLogActivities($_SESSION['user']['user-log']['id'], 'User', 'View User Log Detail');
		
		$this->set('users', $user_logs);
		$this->set('page_title','User Log Detail');
		$this->view('user-log-detail');
	}
	function delete_log($id = 0){
		$objUser = $this->load_model('users');
		$objUser -> deleteLog($id);
		
		$objUser = $this->load_model('users');
		$objUser -> saveUserLogActivities($_SESSION['user']['user-log']['id'], 'User', 'Delete User Log');
		
		$this->redirect('log');
	}
	function get_raw_password(){
		$pwd = getURL('pass');
		echo b64decode($pwd);
	}
}
?>